package com.example.review;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.review.ListView.Fruit;
import com.example.review.ListView.FruitAdapter;

import java.util.ArrayList;
import java.util.List;

public class chapterThree2Activity extends AppCompatActivity {


   /**
    *这是listview的简单使用，listview子项就是一个简单的文本
    * */
//    //定义了一个模拟数据的数组
//    private String[] data = { "Apple", "Banana", "Orange", "Watermelon", "Pear",
//            "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Apple",
//            "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple",
//            "Strawberry", "Cherry", "Mango" };
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_chapter_three2);
//        //数组中的数据是无法直接传递给ListView的，我们还需要借助适配器来完成
//        //三个参数
//        /*
//        当前上下文、 ListView子项布局的id，以及要适配的数据
//        android.R.layout.simple_list_item_1 作为ListView子项布局的id，这是一个Android 内置的布局文件，里面只有一个TextView，可用于简单地显示一段文本
//
//        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
//        ListView listView=findViewById(R.id.list_view);
//        //将构建好的适配器对象传递进去
//        listView.setAdapter(adapter);
//    }

    /**
     *这是listview的进阶使用，定制了listview的子项，包含名字和图片
     * */
    public List<Fruit> fruitList =new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_three2);
        initFruit();
        FruitAdapter adapter =new FruitAdapter(chapterThree2Activity.this,R.layout.listview_item,fruitList);
        ListView listView =findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        //设置点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Fruit fruit=fruitList.get(position);
                Toast.makeText(chapterThree2Activity.this, "这个水果的名字是"+fruit.getName(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void initFruit(){

        for (int i = 0; i <5 ; i++) {
            Fruit apple =new Fruit("红苹果",R.drawable.tx);
            fruitList.add(apple);
            Fruit banana = new Fruit("Banana", R.drawable.tx);
            fruitList.add(banana);
            Fruit orange = new Fruit("Orange", R.drawable.tx);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("Watermelon", R.drawable.tx);
            fruitList.add(watermelon);
            Fruit pear = new Fruit("Pear", R.drawable.tx);
            fruitList.add(pear);
        }
    }
}